#include<stdio.h>
#include<vector>
#include<iostream>
 
__global__ void add( int *mat, int *solveMat, int *ret ) {
    //int tid = blockIdx.x;    // this thread handles the data at its thread id
	int nYIndex = 0;
    for(int i = 0; i < 3000; i++)
    {
        for(int j = 0; j < 3000; j++)
        {
            nYIndex = 3000 * j;
            ret[i] += solveMat[j] * mat[nYIndex + i];
        }
    }
    
    for(int i = 0; i < 3000; i++)
    {
        ret[i] = ret[i] * 1;
    }
}
 
int main( void ) {
    clock_t startT,endT;
    std::vector<int> mat,solveMat,ret;
    int *dev_a, *dev_b, *dev_c;

    mat.resize(9000000,1);
    solveMat.resize(3000,1);
    ret.resize(3000,0);
 
    // allocate the memory on the GPU
    cudaMalloc( (void**)&dev_a, mat.size() * sizeof(int) );
    cudaMalloc( (void**)&dev_b, solveMat.size() * sizeof(int) );
    cudaMalloc( (void**)&dev_c, ret.size() * sizeof(int) );
 
    // copy the arrays 'a' and 'b' to the GPU
    cudaMemcpy( dev_a, mat.data(), mat.size() * sizeof(int),
                              cudaMemcpyHostToDevice );
    cudaMemcpy( dev_b, solveMat.data(), solveMat.size() * sizeof(int),
                              cudaMemcpyHostToDevice );
    
    startT = clock();
    add<<<1,1500>>>( dev_a, dev_b, dev_c );
 
    // copy the array 'c' back from the GPU to the CPU
    cudaMemcpy( ret.data(), dev_c, solveMat.size() * sizeof(int),
                              cudaMemcpyDeviceToHost );
 
    // display the results
    /*
    for (int i=0; i<N; i++) {
        printf( "%d + %d = %d\n", a[i], b[i], c[i] );
    }
    */
    for(int i = 0; i < 10; i++)
    {
        printf("data is:%d\n",ret[i]);
    }
    endT = clock();
    clock_t usedTime = endT - startT;
    std::cout<<"cost time:"<<usedTime<<std::endl;
    
    // free the memory allocated on the GPU
    cudaFree( dev_a );
    cudaFree( dev_b );
    cudaFree( dev_c );
    return 0;
}